package b面向对象.day4.demo3_值和地址传递;

public class Person {
    int age;

    public Person(int age) {
        this.age = age;
    }

    public static void main1(String[] args) {
        int a=10;
        int b=20;
        transferValue(a,b);
        System.out.println("方法外，a="+a+",b="+b);
    }

    //基本类型传参数时，传递的是数值，而非地址
    static void transferValue(int a){
        a=30;
        System.out.println("方法内：a="+a);
    }
    static void transferValue(int a,int b){
        int tmp=a;
        a=b;
        b=tmp;
        System.out.println("方法内：a="+a+",b="+b);
    }

    public static void main2(String[] args) {
        Person p1=new Person(10);
        Person p2=new Person(20);
        reference(p1,p2);
        System.out.println("方法外，p1.age="+p1.age+",p2.age="+p2.age);
    }
    static void reference(Person p1,Person p2){
        int tmp=p1.age;//tmp=10
        p1.age=p2.age;//p11.age=20
        p2.age=tmp;//p22.age=10
        System.out.println("方法内，p1.age="+p1.age+",p2.age="+p2.age);
    }

    public static void main3(String[] args) {
        String s1="abc";//119
        String s2="123";//228
        swap(s1,s2);
        System.out.println("方法外：s1="+s1+",s2="+s2);
    }
    static void swap(String s1,String s2){
        String tmp=s1;
        s1=s2;
        s2=tmp;
        System.out.println("方法内：s1="+s1+",s2="+s2);
    }

    public static void main(String[] args) {
        String s1="good";
        char[]arr={'a','b','c'};
        change(s1,arr);
        System.out.print(s1+"and");
        System.out.println(arr);

    }
    static void change(String s,char[]ch){
        s="test ok";
        ch[0]='g';
    }

}
